﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBLibrary;

public partial class pages_member_CustomerDetailPage : System.Web.UI.Page
{
    private string mCustomerId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(this.Request.QueryString["action"]) &&
            this.Request.QueryString["action"].Equals("v", StringComparison.OrdinalIgnoreCase))
        {
            mCustomerId = this.Request.QueryString["id"];
            cid.Value = mCustomerId;
            DB db = PoseidonDBHelper.UserDB;
            CustomerDetail cd = db.Select<CustomerDetail>(new DBParam("CustomerId", mCustomerId))[0];
            this.CustomerNameTextBox.Text = cd.Name;
            this.EmailTextBox.Text = cd.EMail;
            this.PhoneTextBox.Text = cd.Phone;
            this.LevelDropDownList.SelectedIndex = cd.Level - 1;
            if (cd.Gender.Equals("男"))
            {
                this.GenderDropDownList.SelectedIndex = 0;
            }
            else
            {
                this.GenderDropDownList.SelectedIndex = 1;
            }
            //EnableEditMode(false);
        }
    }

    private void EnableEditMode(bool isEditMode)
    {
        this.CustomerNameTextBox.Enabled = isEditMode;
        this.EmailTextBox.Enabled = isEditMode;
        this.PhoneTextBox.Enabled = isEditMode;
        this.LevelDropDownList.Enabled = isEditMode;
        this.GenderDropDownList.Enabled = isEditMode;
    }

    protected void DeleteImageButton_Click(object sender, ImageClickEventArgs e)
    {
        List<CustomerExtendKeyValue> list = CustomerExtendFieldHelper.GetCustomerDetailInfos(long.Parse(mCustomerId));
        DB db = PoseidonDBHelper.UserDB;
        foreach (CustomerExtendKeyValue kv in list)
        {
            db.Delete<CustomerExtendDetail>(new DBParam("CustomerId", long.Parse(mCustomerId)), new DBParam("Row", kv.Row));
        }
        db.Delete<CustomerDetail>(new DBParam("CustomerId", mCustomerId));
        Response.Redirect("~/pages/member/MemberOperation.aspx");
    }
    
    protected void ModifyImageButton_Click(object sender, ImageClickEventArgs e)
    {
        this.Response.Redirect(string.Format("~/pages/member/AddNewMember.aspx?action=m&id={0}", mCustomerId));
    }
}